package com.bdth.bdthiss.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.bdthiss.entity.Category;
import com.bdth.bdthiss.entity.Excelgoods;
import com.bdth.bdthiss.mapper.CategoryMapper;
import com.bdth.bdthiss.service.CategoryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bdth.bdthiss.vo.Pagination;
import com.google.common.base.Joiner;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品分类表 服务实现类
 * </p>
 *
 * @author LGP
 * @since 2020-04-06
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<Category> listPages(Pagination pagination, Map<String, Object> query) {

        EntityWrapper<Category> entityWrapper = new EntityWrapper<>();
        String type = (String) query.get("type");

        //一级类型
        if(type!=null && type.equals("top")){
            entityWrapper.eq("parent_id",0);
        }else if(type!=null && type.equals("sub")){
            //三级类型
            EntityWrapper<Category> entity = new EntityWrapper<>();
            entity.eq("parent_id",0);
            List<Category> tops = categoryMapper.selectList(entity);
            List<Integer> ids = tops.stream().map(Category::getId).collect(Collectors.toList());
            String strId = Joiner.on(",").join(ids);
            System.out.println(strId);
            //查询第二节
            EntityWrapper<Category> categoryEntityWrapper = new EntityWrapper<>();
            categoryEntityWrapper.in("parent_id",strId);
            List<Category> second = categoryMapper.selectList(categoryEntityWrapper);
            List<Integer> secondids = second.stream().map(Category::getId).collect(Collectors.toList());
            String secondStrIds = Joiner.on(",").join(secondids);
            entityWrapper.in("parent_id",secondStrIds);
        }else{
            //二级类型
            EntityWrapper<Category> entity = new EntityWrapper<>();
            entity.eq("parent_id",0);
            List<Category> tops = categoryMapper.selectList(entity);
            List<Integer> ids = tops.stream().map(Category::getId).collect(Collectors.toList());
            String strId = Joiner.on(",").join(ids);
            System.out.println(strId);
            entityWrapper.in("parent_id",strId);
        }

        if(query.get("name")!=null){
            entityWrapper.like("name", (String) query.get("name"));
        }
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Category> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        List<Category> items = categoryMapper.selectPage(page,entityWrapper);
        page.setRecords(items);
        return page;
    }
}
